<template>
  <div class="llmView">
    <input v-model="inputText" placeholder="Enter text" />
    <button @click="quickChat()">Send to Coze AI</button>
    <p>Response from Coze AI: {{ response }}</p>
  </div>
</template>
 
<script>
import axios from "axios";
import { CozeAPI, COZE_COM_BASE_URL, ChatEventType, RoleType } from "@coze/api";

export default {
  data() {
    return {
      inputText: "",
      response: "",
    };
  },
  methods: {
    // 对话
    async quickChat() {
      this.response=""
      // Initialize client with your Personal Access Token
      const client = new CozeAPI({
        token:
          "pat_La8cgWA7YHSzvpfSTPSNFnW7P4mrmPTHZVrXu5Nudp3WDFjhiq4NyZ5m4iGG79jl", // Get your PAT from https://www.coze.com/open/oauth/pats
        // or
        // token: async () => {
        //   // refresh token if expired
        //   return "Bearer pat_La8cgWA7YHSzvpfSTPSNFnW7P4mrmPTHZVrXu5Nudp3WDFjhiq4NyZ5m4iGG79jl";
        // },
        // Authorization:
        //   "Bearer pat_La8cgWA7YHSzvpfSTPSNFnW7P4mrmPTHZVrXu5Nudp3WDFjhiq4NyZ5m4iGG79jl", // 替换为你的API密钥
        // baseURL: "http://api.wlai.vip",
        // baseURL:COZE_COM_BASE_URL,
        baseURL: "https://api.coze.cn",
        allowPersonalAccessTokenInBrowser: true, 
      });
      //console.log(client);
      const stream = await client.chat.stream({
        bot_id: "7460766089165864995",
        additional_messages: [
          {
            content: this.inputText,
            content_type: "text",
            role: "user",
          },
        ],
      });
      //console.log(stream);
      for await (const part of stream) {
        if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
          //console.log(part.data.content);
          this.response += part.data.content; //实时响应
          // process.stdout.write(part.data.content); // 实时响应
        }
      }
    },
    // 获取智能体配置
    async getRobot(){
    }
  },
};
</script>
<style>
.llmView{
  width: 100vw;
  height: calc(100vh - 120px);
  border: 1px solid red;
}
</style>